home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / dev / c / LEDA_GUI.lha / files.lha / src / amiga / _awindow.c next >
Encoding:
C/C++ Source or Header  |  1995-03-27  |  33.3 KB  |  1,558 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  _awindow.c
  7. +  written by Kay Drangmeister <K.Drangmeister@insider.sub.de>
  8. +
  9. *******************************************************************************/
  10.  
  11.  
  12.  
  13. // basic graphic routines for libWx declared in <LEDA/impl/x_basic.h>
  14. // implemented using Amiga functions
  15.  
  16.  
  17. #include "impl_x_basic.h"
  18.  
  19.  
  20. #include <math.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <values.h>
  25.  
  26. /* limits.h defines this: */
  27. #undef LONGBITS
  28. #undef BITSPERBYTE
  29. #undef MAXINT
  30. #undef MININT
  31. extern "C" {
  32. #include <exec/memory.h>
  33. #include <exec/libraries.h>
  34. #include <intuition/intuition.h>
  35. #include <intuition/classusr.h>
  36. #include <intuition/screens.h>
  37. #include <graphics/text.h>
  38. #include <dos/dos.h>
  39.  
  40. #include <inline/stubs.h>
  41. #include <inline/exec.h>
  42. #include <inline/graphics.h>
  43. #include <inline/intuition.h>
  44. #include <inline/diskfont.h>
  45. #include <inline/dos.h>
  46. #include <clib/alib_protos.h>
  47. }
  48.  
  49. #undef TRACE
  50.  
  51. // window title, max strlen
  52. #define TITLELEN    99
  53.  
  54. //#define TRACE
  55.  
  56.  
  57. #ifdef TRACE
  58. #define DPRINT(x) printf x
  59. #else
  60. #define DPRINT(x)
  61. #endif
  62. #define EPRINT(x)
  63. #define CPRINT(x) printf x
  64.  
  65. typedef struct {
  66.     struct Window *        win;
  67.     struct RastPort *    rp;
  68.     int                    bg_col;
  69.     WORD                borl,bort;
  70.     struct TmpRas        tr;
  71.     UBYTE *                trb;    /* tmpras buffer */
  72.     ULONG                trs;    /* tmpras size */
  73.     ULONG                rpn;    /* update indicator for color, drawmode, etc. */
  74.     char                Title[TITLELEN+1];
  75. } IWindow;
  76.  
  77.  
  78. typedef struct {
  79.     ULONG    width;
  80.     ULONG    height;
  81.     ULONG    depth;
  82.     ULONG    modeID;
  83.     ULONG    Overscan;
  84.     ULONG    AutoScroll;
  85.     struct    TextAttr TextFontAttr;
  86.     struct    TextAttr BoldFontAttr;
  87.     struct    TextAttr MsgFontAttr;
  88.     STRPTR    X11_Color_Base;
  89.     BOOL    Picasso_Kludge;
  90. } LedaPrefs;
  91.  
  92. static LedaPrefs pr;
  93.  
  94. struct DosLibrary *DOSBase=NULL;
  95. struct IntuitionBase *IntuitionBase=NULL;
  96. struct GfxBase *GfxBase=NULL;
  97. struct Library * DiskfontBase=NULL;
  98. static struct Screen * ascr=NULL;
  99. static struct RastPort allrp;
  100. //static struct ColorMap * acm=NULL;
  101. static ULONG allrpn=0;
  102. struct IntuiMessage ievent;
  103. struct IWindow * ieventwin;
  104. static BOOL eventagain=FALSE;
  105.  
  106. #define forall_windows(x) \
  107.     IWindow **wp=warray; \
  108.     for(wp=warray,x=*wp++;x!=NULL;x=*wp++)
  109.  
  110. struct IWindow * warray[16]={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
  111. ULONG warrcnt=0;
  112. ULONG wsigmask=0;    // 1<<w->win->UserPort->mp_SigBit of every window
  113.  
  114. struct TextAttr DefFontAttr=  {"topaz.font",8,FS_NORMAL,FPF_ROMFONT};
  115. //struct TextAttr TextFontAttr= {"courier.font",13,FS_NORMAL,FPF_DISKFONT};
  116. //struct TextAttr BoldFontAttr= {"courier.font",13,FSF_BOLD,FPF_DISKFONT};
  117. //struct TextAttr MsgFontAttr=  {"courier.font",15,FS_NORMAL,FPF_DISKFONT};
  118. static struct TextFont * def_font=NULL;
  119. static struct TextFont * text_font=NULL;
  120. static struct TextFont * bold_font=NULL;
  121. static struct TextFont * mesg_font=NULL;
  122. static struct TextFont * current_font=NULL;
  123. static int mesg_char_width;
  124. static int bold_char_width;
  125. static int text_char_width;
  126. static int current_char_width;
  127.  
  128. static int LINESTYLE = 0;
  129. static int MODE      = 0;
  130. static int COLOR     = 1;
  131.  
  132. static int MAX_COLORS;
  133. const int MAX_MAX_COLORS=256;    // only supporting 8 bit screens...
  134. static unsigned long color_pix[MAX_MAX_COLORS];
  135. static char* color_name[MAX_MAX_COLORS];
  136. static int color_count= 0;
  137.  
  138. static UWORD pens[]={14,0,1,13,15,2,1,14,3,9,14,15,0xFFFF};
  139.  
  140. static int a_drawmode;
  141. static int a_linewidth;
  142. static int a_linestyle;
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. #define SetLineStyle(rp,style)    (rp)->LinePtrn=(style)
  150. #define SOLID    0xFFFF
  151. #define DASHED    0xF0F0
  152. #define DOTTED    0x5555
  153. #define SetMask(rp,mask)        (rp)->Mask=(mask)
  154. #define ResetMask(rp)            (rp)->Mask=~0
  155. #define SetLineWidth(i)            a_linewidth=i
  156.  
  157. inline void ALine(struct RastPort * rp,WORD x1,WORD y1,WORD x2,WORD y2)
  158. {
  159.     int i;
  160.     if(pr.Picasso_Kludge)
  161.     {
  162.         // lines must be drawn in one direction only
  163.         // since line(a,b) != line(b,a)
  164.         if(x1>x2) {WORD t=x1;x1=x2;x2=t;t=y1;y1=y2;y2=t;}
  165.     }
  166.     if(a_linewidth==1)
  167.     {
  168.         Move(rp,x1,y1);
  169.         Draw(rp,x2,y2);
  170.     }
  171.     else
  172.     {
  173.         for(i=0;i<a_linewidth;i++)
  174.         {
  175.             Move(rp,x1+i,y1);
  176.             Draw(rp,x2+i,y2);
  177.             Move(rp,x1-i,y1);
  178.             Draw(rp,x2-i,y2);
  179.             Move(rp,x1,y1+i);
  180.             Draw(rp,x2,y2+i);
  181.             Move(rp,x1,y1-i);
  182.             Draw(rp,x2,y2-i);
  183.  
  184.             Move(rp,x1+i,y1+i);
  185.             Draw(rp,x2+i,y2+i);
  186.             Move(rp,x1-i,y1+i);
  187.             Draw(rp,x2-i,y2+i);
  188.             Move(rp,x1+i,y1-i);
  189.             Draw(rp,x2+i,y2-i);
  190.             Move(rp,x1-i,y1-i);
  191.             Draw(rp,x2-i,y2-i);
  192.         }
  193.     }
  194. }
  195.  
  196. inline void ARect(struct RastPort * rp,WORD x1,WORD y1,WORD x2,WORD y2)
  197. {
  198.     int i;
  199.     if(a_linewidth==1)
  200.     {
  201.         Move(rp,x1,y1);
  202.         Draw(rp,x1,y2);
  203.         Draw(rp,x2,y2);
  204.         Draw(rp,x2,y1);
  205.         Draw(rp,x1,y1);
  206.     }
  207.     else
  208.     {
  209.         for(i=0;i<a_linewidth/2;i++)
  210.         {
  211.             Move(rp,x1+i,y1+i);
  212.             Draw(rp,x1+i,y2-i);
  213.             Draw(rp,x2-i,y2-i);
  214.             Draw(rp,x2-i,y1+i);
  215.             Draw(rp,x1+i,y1+i);
  216.  
  217.             Move(rp,x1-i,y1-i);
  218.             Draw(rp,x1-i,y2+i);
  219.             Draw(rp,x2+i,y2+i);
  220.             Draw(rp,x2+i,y1-i);
  221.             Draw(rp,x1-i,y1-i);
  222.         }
  223.     }
  224. }
  225.  
  226. inline void SetFg(struct RastPort * rp,UBYTE pen)
  227. {
  228.     SetAPen(rp,pen);
  229. /*
  230.     if(rp->DrawMode==COMPLEMENT)
  231.         SetMask(rp,pen&7);
  232.     else
  233.         ResetMask(rp);
  234. */
  235. }
  236.  
  237. inline void SetDM(struct RastPort * rp,UBYTE dm)
  238. {
  239.     SetDrMd(rp,dm);
  240. /*
  241.     if(dm==COMPLEMENT)
  242.         SetMask(rp,(rp->FgPen)&7);
  243.     else
  244.         ResetMask(rp);
  245. */
  246. }
  247.  
  248. inline void SetDMFg(struct RastPort * rp,UBYTE dm,UBYTE pen)
  249. {
  250.     SetDrMd(rp,dm);
  251.     SetAPen(rp,pen);
  252. /*
  253.     if(dm==COMPLEMENT)
  254.         SetMask(rp,pen&7);
  255.     else
  256.         ResetMask(rp);
  257. */
  258. }
  259.  
  260. inline void ResetDMFg(struct RastPort * rp)
  261. {
  262.     UBYTE dm=a_drawmode;
  263.     UBYTE pen=color_pix[COLOR];
  264.     SetDrMd(rp,dm);
  265.     SetAPen(rp,pen);
  266. /*
  267.     if(dm==COMPLEMENT)
  268.         SetMask(rp,pen&7);
  269.     else
  270.         ResetMask(rp);
  271. */
  272. }
  273.  
  274.  
  275. #define UPDATE(win) \
  276.     if(((IWindow *)(win))->rpn != allrpn) \
  277.         UpdateRP((IWindow *)(win));
  278.  
  279. #define UPDATEON allrpn++
  280.  
  281. static void UpdateRP(struct IWindow * w)
  282. {
  283.     SetDrMd(w->rp,a_drawmode);
  284.     SetAPen(w->rp,color_pix[COLOR]);
  285.     DPRINT(("  UPDATE:using pen #%d\n",color_pix[COLOR]));
  286.     SetLineStyle(w->rp,a_linestyle);
  287.     w->rpn=allrpn;
  288. }
  289.  
  290. //
  291. // prefs einlesen
  292. //
  293.  
  294. ULONG NamedModeID(char * name)
  295. {
  296.     static unsigned char buf[200];
  297.     ULONG id;
  298.     APTR hd;
  299.     id=INVALID_ID;
  300.     for(;;)
  301.     {
  302.         id=NextDisplayInfo(id);
  303.         if(id==INVALID_ID)
  304.             break;
  305.         hd=FindDisplayInfo(id);
  306.         GetDisplayInfoData(hd,buf,199,DTAG_NAME,0);
  307.         if(strcmp((char *)&(((struct NameInfo *)buf)->Name),name)==0)
  308.         {
  309.             return id;
  310.         }
  311.     }
  312.     return 0;
  313. }
  314.  
  315. void ReadPrefs(void)
  316. {
  317.     static char buf[2048];
  318.     static char *entry[17];
  319.     BPTR fh;
  320.     int pref_lines;
  321.     strcpy(buf,"17\n|1024\n|768\n|5\n|PAL:LowRes\n|1\n|0\n|courier.font\n|13\n|0\n|courier.font\n|13\n|1\n|courier.font\n|15\n|0\n|usr:lib/X11/rgb.txt\n|0\n");
  322.     DPRINT(("reading prefs...\n"));
  323.     if((fh=Open("ENV:LEDAGUI",MODE_OLDFILE))!=NULL)
  324.     {
  325.         Read(fh,buf,2047);
  326.         Close(fh);
  327.         DPRINT(("  loaded\n"));
  328.     }
  329.     char * s=buf;
  330.     char c;
  331.     while((c=*s)!='#')
  332.     {
  333.         if(c=='\n')
  334.             *s=0;
  335.         s++;
  336.     }
  337.     s=buf;
  338.     pref_lines=atol(s);
  339.     for(int i=0;i<pref_lines;i++)
  340.     {
  341.         while(*s++!='|');
  342.         entry[i]=s;
  343.     }
  344.     pr.width=atol(entry[0]);
  345.     pr.height=atol(entry[1]);
  346.     pr.depth=atol(entry[2]);
  347.     pr.modeID=NamedModeID(entry[3]);
  348.     pr.Overscan=atol(entry[4]);
  349.     pr.AutoScroll=atol(entry[5]);
  350.     pr.TextFontAttr.ta_Name=entry[6];
  351.     pr.TextFontAttr.ta_YSize=atol(entry[7]);
  352.     pr.TextFontAttr.ta_Style=atol(entry[8]);
  353.     pr.TextFontAttr.ta_Flags=FPF_DISKFONT;
  354.     pr.BoldFontAttr.ta_Name=entry[9];
  355.     pr.BoldFontAttr.ta_YSize=atol(entry[10]);
  356.     pr.BoldFontAttr.ta_Style=atol(entry[11]);
  357.     pr.BoldFontAttr.ta_Flags=FPF_DISKFONT;
  358.     pr.MsgFontAttr.ta_Name=entry[12];
  359.     pr.MsgFontAttr.ta_YSize=atol(entry[13]);
  360.     pr.MsgFontAttr.ta_Style=atol(entry[14]);
  361.     pr.MsgFontAttr.ta_Flags=FPF_DISKFONT;
  362.     pr.X11_Color_Base=entry[15];
  363.     pr.Picasso_Kludge=atol(entry[16]);
  364. }
  365.  
  366.  
  367.  
  368.  
  369. static int get_char_width(struct TextFont * f)
  370. {
  371.     DPRINT(("- get_char_width()=%d\n",f->tf_XSize));
  372.     return f->tf_XSize;
  373. }
  374.  
  375. void open_display(void)
  376. {
  377.     DPRINT(("- open_display()\n"));
  378.     if(ascr!=NULL) return;
  379.  
  380.     DOSBase=(struct DosLibrary *)OpenLibrary((UBYTE *)"dos.library",37);
  381.     IntuitionBase=(struct IntuitionBase *)OpenLibrary((UBYTE *)"intuition.library",37);
  382.     GfxBase=(struct GfxBase *)OpenLibrary((UBYTE *)"graphics.library",37);
  383.     DiskfontBase=OpenLibrary((UBYTE *)"diskfont.library",37);
  384.     if((DOSBase==NULL)||(IntuitionBase==NULL)||(GfxBase==NULL)||(DiskfontBase==NULL))
  385.     {
  386.         fprintf(stderr,"Can\'t open libraries\n");
  387.         abort();
  388.     }
  389.     ReadPrefs();
  390.  
  391.     ascr=OpenScreenTags(NULL,
  392.             {SA_Depth,pr.depth},
  393.             {SA_Width,pr.width},
  394.             {SA_Height,pr.height},
  395.             {SA_DisplayID,pr.modeID},
  396.             {SA_Overscan,pr.Overscan},
  397.             {SA_AutoScroll,pr.AutoScroll},
  398.             {SA_Type,CUSTOMSCREEN},
  399.             {SA_Pens,(ULONG)pens},
  400.             //{SA_Colors,(ULONG)&coltab},
  401.             {SA_Title,(ULONG)"LEDA GUI © 1994-95 by Kay Drangmeister"},
  402.             //{SA_PubName,(ULONG)"LEDA_pubscreen"},
  403.             {TAG_DONE});
  404.     if(!ascr)
  405.     {
  406.         fprintf(stderr,"Can\'t open Screen\n");
  407.         abort();
  408.     }
  409.     MAX_COLORS=1<<pr.depth;
  410.     //PubScreenStatus(ascr,0);
  411.     //acm=GetColorMap(MAX_COLORS);
  412.     //acm=ascr->ViewPort.ColorMap;
  413.     //if(!acm)
  414.     //{
  415.     //    fprintf(stderr,"Can\'t open Colormap\n");
  416.     //    abort();
  417.     //}
  418.     //if(AttachPalExtra(acm,&ascr->ViewPort))
  419.     //{
  420.     //    fprintf(stderr,"Can\'t open PaletteExtra\n");
  421.     //    abort();
  422.     //}
  423.     InitRastPort(&allrp);
  424.  
  425.     def_font=(struct TextFont *)OpenDiskFont(&DefFontAttr);
  426.     text_font=(struct TextFont *)OpenDiskFont(&pr.TextFontAttr);
  427.     if (!text_font)
  428.     {
  429.         fprintf(stderr,"Cannot open text font\n");
  430.         text_font=def_font;
  431.     }
  432.     bold_font=(struct TextFont *)OpenDiskFont(&pr.BoldFontAttr);
  433.     if (!bold_font)
  434.     {
  435.         fprintf(stderr,"Cannot open bold font\n");
  436.         bold_font=def_font;
  437.     }
  438.     mesg_font=(struct TextFont *)OpenDiskFont(&pr.MsgFontAttr);
  439.     if (!mesg_font)
  440.     {
  441.         fprintf(stderr,"Cannot open message font\n");
  442.         mesg_font=def_font;
  443.     }
  444.     SetFont(&allrp,text_font);
  445.  
  446.     //XSetLineAttributes(display,gc,1,LineSolid,CapButt,JoinMiter);
  447.  
  448.     color_count=0;
  449.     new_color("white");          // 0: white
  450.     new_color("black");          // 1: black
  451.     new_color("red");            // 2: red
  452.     new_color("green");          // 3: green
  453.     new_color("blue");           // 4: blue
  454.     new_color("yellow");         // 5: yellow
  455.     new_color("purple");         // 6: violet
  456.     new_color("darkorange");     // 7: orange
  457.     new_color("cyan");           // 8: cyan
  458.     new_color("sienna");         // 9: brown
  459.     new_color("magenta");        //10: pink
  460.     new_color("ForestGreen");    //11: darkgreen
  461.     new_color("CornflowerBlue"); //12: darkblue
  462.     new_color("grey75");         //13: grey1
  463.     new_color("grey60");         //14: grey2
  464.     new_color("grey45");         //15: grey3
  465.  
  466.     set_color(1);
  467.     set_mode(xor_mode);
  468.     set_line_style(solid);
  469.     set_line_width(1);
  470.  
  471.     text_char_width=get_char_width(text_font);
  472.     bold_char_width=get_char_width(bold_font);
  473.     mesg_char_width=get_char_width(mesg_font);
  474.     current_char_width=text_char_width;
  475.     current_font=text_font;
  476.     UPDATEON;
  477. }
  478.  
  479. void set_palette(int,int,int,int)   { /* not implemented */ }
  480.  
  481. void set_redraw(LWindow,void (*)()) { /* not implemented */ }
  482.  
  483. typedef struct {
  484.     char *    name;
  485.     ULONG    r,g,b;
  486. } x11rgb;
  487.  
  488. static x11rgb * LoadColorTable(char * f)
  489. {
  490.     BPTR fh;
  491.     struct FileInfoBlock * fib;
  492.     x11rgb * coltab=NULL;
  493.     static char line[160];
  494.     fib=(struct FileInfoBlock *)AllocDosObject(DOS_FIB,TAG_DONE);
  495.     if(fib)
  496.     {
  497.         if((fh=Open(f,MODE_OLDFILE))!=NULL)
  498.         {
  499.             ExamineFH(fh,fib);
  500.             int len=fib->fib_Size;
  501.             char *buf=new char[len+1];
  502.             if(buf)
  503.             {
  504.                 Read(fh,buf,len);
  505.                 DPRINT(("  loaded\n"));
  506.                 buf[len]=0;
  507.                 char *cp=buf;
  508.                 char c;
  509.                 int l,lines=0;
  510.                 while((c=*cp++)!=0)
  511.                 {
  512.                     if(c=='\n')
  513.                     {
  514.                         cp[-1]=0;
  515.                         lines++;
  516.                     }
  517.                 }
  518.                 coltab=new x11rgb[lines+1];
  519.                 if(coltab)
  520.                 {
  521.                     cp=buf;
  522.                     for(l=0;l<lines;l++)
  523.                     {
  524.                         strcpy(line,cp);
  525.                         cp+=strlen(line)+1;
  526.                         char *ip=line;
  527.                         while(*ip==' '||*ip=='\t') ip++;
  528.                         coltab[l].r = atol(ip)*0x01010101;
  529.                         while(*ip!=' '&&*ip!='\t') ip++;
  530.                         while(*ip==' '||*ip=='\t') ip++;
  531.                         coltab[l].g = atol(ip)*0x01010101;
  532.                         while(*ip!=' '&&*ip!='\t') ip++;
  533.                         while(*ip==' '||*ip=='\t') ip++;
  534.                         coltab[l].b = atol(ip)*0x01010101;
  535.                         while(*ip!=' '&&*ip!='\t') ip++;
  536.                         while(*ip==' '||*ip=='\t') ip++;
  537.                         coltab[l].name = new char[strlen(ip)+1];
  538.                         strcpy(coltab[l].name,ip);
  539.                     }
  540.                     coltab[l].r = 0;
  541.                     coltab[l].g = 0;
  542.                     coltab[l].b = 0;
  543.                     coltab[l].name = NULL;
  544.                 }
  545.             }
  546.             Close(fh);
  547.         }
  548.         FreeDosObject(DOS_FIB,fib);
  549.     }
  550.     return coltab;
  551. }
  552.  
  553. x11rgb * getColor(const char* name)
  554. {
  555.     DPRINT(("- getColor(%s)=",name));
  556.     static x11rgb *ColorTable=NULL;
  557.     static x11rgb srgb[]={
  558.         {"white",            0xffffffff,0xffffffff,0xffffffff},
  559.         {"black",            0x00000000,0x00000000,0x00000000},
  560.         {"red",                0xffffffff,0x00000000,0x00000000},
  561.         {"green",            0x00000000,0xffffffff,0x00000000},
  562.         {"blue",            0x00000000,0x00000000,0xffffffff},
  563.         {"yellow",            0xffffffff,0xffffffff,0x00000000},
  564.         {"purple",            0x88888888,0x00000000,0xffffffff},
  565.         {"darkorange",        0xdddddddd,0xaaaaaaaa,0x00000000},
  566.         {"cyan",            0x00000000,0xffffffff,0xffffffff},
  567.         {"sienna",            0x55555555,0x44444444,0x00000000},
  568.         {"magenta",            0xffffffff,0x00000000,0xffffffff},
  569.         {"forestgreen",        0x88888888,0xffffffff,0x00000000},
  570.         {"cornflowerblue",    0x88888888,0x55555555,0xffffffff},
  571.         {"grey75",            0xcccccccc,0xcccccccc,0xcccccccc},
  572.         {"grey60",            0x99999999,0x99999999,0x99999999},
  573.         {"grey45",            0x72727272,0x72727272,0x72727272},
  574.         {NULL,                0x00000000,0x00000000,0x00000000},
  575.     };
  576.  
  577.     if(ColorTable==NULL)
  578.     {
  579.         if((ColorTable=LoadColorTable(pr.X11_Color_Base))==NULL)
  580.         {
  581.             ColorTable=srgb;
  582.         }
  583.     }
  584.  
  585.     x11rgb * r=NULL;
  586.     x11rgb * i;
  587.     for(i=ColorTable;i->name!=NULL;i++)
  588.     {
  589.         r=i;
  590.         if(strcmp(name,i->name)==0)
  591.             break;
  592.     }
  593.     DPRINT(("%lx\n",r));
  594.     if(r==NULL) r=&srgb[2];
  595.     return r;
  596. }
  597.  
  598. int MyObtainPen(x11rgb * col)
  599. {
  600.     // should be something like:
  601.     // return ObtainBestPen(acm,col->r,col->g,col->b,OBP_Precision,PRECISION_EXACT,TAG_DONE);
  602.     static int MyObtainPenNum=0;
  603.     int x=MyObtainPenNum;
  604.     if(x<MAX_COLORS)
  605.     {
  606.         MyObtainPenNum++;
  607.         return x;
  608.     }
  609.     else
  610.         return -1;
  611. }
  612.  
  613. void MySetColor(int n,x11rgb * col)
  614. {
  615.     if(((struct Library *)GfxBase)->lib_Version>=39)
  616.     {
  617.         SetRGB32(&ascr->ViewPort,n,col->r,col->g,col->b);
  618.         if(2*n<MAX_COLORS)    // if we are in the lower halve, we set corresponding col to inverse (for exor crap)
  619.         {
  620.             SetRGB32(&ascr->ViewPort,MAX_COLORS-1-n,~col->r,~col->g,~col->b);
  621.         }
  622.     }
  623.     else
  624.     {
  625.         SetRGB4(&ascr->ViewPort,n,col->r>>28,col->g>>28,col->b>>28);
  626.         if(2*n<MAX_COLORS)    // if we are in the lower halve, we set corresponding col to inverse (for exor crap)
  627.         {
  628.             SetRGB4(&ascr->ViewPort,MAX_COLORS-1-n,~col->r>>28,~col->g>>28,~col->b>>28);
  629.         }
  630.     }
  631. }
  632.  
  633. int new_color(const char* name)
  634. {
  635.     DPRINT(("- new_color(%s)\n",name));
  636.  
  637.     open_display();
  638.  
  639.     // first test if color has been allocated before
  640.     for(int i=0; i < color_count; i++)
  641.         if (strcmp(color_name[i],name)==0)
  642.             break;
  643.     if(i<color_count)
  644.         return i;
  645.  
  646.     // if not, try to allocate it
  647.     if (color_count == MAX_COLORS) return 0;
  648.  
  649.     x11rgb * col;
  650.  
  651.     if (pr.depth==1 && strcmp(name,"white")!=0)
  652.         col=getColor("black");
  653.     else
  654.         col=getColor(name);
  655.  
  656.     int n=MyObtainPen(col);
  657.     if(n==-1) return 0;
  658.  
  659.     MySetColor(n,col);
  660.     DPRINT(("  pen %ld=rgb(%ld,%ld,%ld)\n",n,col->r,col->g,col->b));
  661.  
  662.     color_pix[color_count]=n;
  663.     char* p=new char[strlen(name)+1];
  664.     if(p==NULL)
  665.         abort();
  666.     strcpy(p,name);
  667.     color_name[color_count]=p;
  668.     UPDATEON;
  669.     return color_count++;
  670. }
  671.  
  672.  
  673.  
  674. LWindow open_window(int x, int y, int width, int height, int bg_col, const char* header,const char* label)
  675. {
  676.     DPRINT(("- open_window(xy=(%d,%d),wh=(%d,%d),hl=(%s,%s))\n",x,y,width,height,header,label));
  677.     IWindow * w=new IWindow;
  678.     if(w==NULL) abort();
  679.  
  680.     strncpy(w->Title,header,TITLELEN);
  681.     w->win=(struct Window *)OpenWindowTags(NULL,
  682.             {WA_Left,x},{WA_Top,y},
  683.             {WA_InnerWidth,width},{WA_InnerHeight,height},
  684.             {WA_AutoAdjust,TRUE},
  685.             {WA_MinWidth,50},{WA_MinHeight,50},
  686.             //{WA_MaxWidth,10000},{WA_MaxHeight,10000},
  687.             {WA_MouseQueue,3},
  688.             {WA_Flags,WFLG_RMBTRAP|WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_SIZEGADGET|WFLG_SIZEBBOTTOM|WFLG_SMART_REFRESH|WFLG_GIMMEZEROZERO|WFLG_REPORTMOUSE|WFLG_ACTIVATE},
  689.             {WA_IDCMP,IDCMP_MOUSEBUTTONS|IDCMP_MOUSEMOVE|IDCMP_NEWSIZE|IDCMP_RAWKEY|IDCMP_VANILLAKEY},
  690.             {WA_Title,(ULONG)(w->Title)},
  691.             {WA_CustomScreen,(ULONG)ascr},
  692.             {TAG_DONE});
  693.     if(!w->win) abort();
  694.     w->rp=w->win->RPort;
  695.     w->borl=(WORD)(w->win->BorderLeft);
  696.     w->bort=(WORD)(w->win->BorderTop);
  697.     w->trs=RASSIZE(w->win->MaxWidth,w->win->MaxHeight);
  698.     w->trb=AllocRaster(w->win->MaxWidth,w->win->MaxHeight);
  699.     if(!w->trb) abort();
  700.     InitTmpRas(&w->tr,w->trb,w->trs);
  701.     w->rp->TmpRas=&w->tr;
  702.  
  703.     SetDM(w->rp,JAM1);
  704.     w->bg_col=color_pix[bg_col];
  705.     SetRast(w->rp,w->bg_col);
  706.  
  707.     // set w->rp according to allrp ###
  708.     SetFont(w->rp,text_font);
  709.  
  710.     warray[warrcnt++]=w;
  711.     wsigmask|=1<<w->win->UserPort->mp_SigBit;
  712.  
  713.     return (LWindow)w;
  714. }
  715.  
  716.  
  717. int display_width(void)
  718. {
  719.     DPRINT(("- display_width()\n[\n"));
  720.     open_display();
  721.     DPRINT(("]=%ld\n",ascr->Width));
  722.     return ascr->Width;
  723. }
  724.  
  725. int display_height(void)
  726. {
  727.     DPRINT(("- display_height()\n[\n"));
  728.     open_display();
  729.     DPRINT(("]=%ld\n",ascr->Height));
  730.     return ascr->Height;
  731. }
  732.  
  733. int display_depth(void)
  734. {
  735.     DPRINT(("- display_depth()\n[\n"));
  736.     open_display();
  737.     DPRINT(("]=%ld\n",pr.depth));
  738.     return pr.depth;
  739. }
  740.  
  741. void flush_display(void)
  742. {}
  743.  
  744. void close_display()
  745. {
  746.     DPRINT(("- close_display()\n"));
  747.  
  748.     if(ascr)
  749.     {
  750.         for(int i=0;i<color_count;i++)
  751.             delete color_name[i];
  752.         CloseFont(mesg_font);
  753.         CloseFont(bold_font);
  754.         CloseFont(text_font);
  755.         CloseFont(def_font);
  756.         //FreeColorMap(acm);
  757.         //PubScreenStatus(ascr,PSNF_PRIVATE); //wait for all visitors ###
  758.         //while(!CloseScreen(ascr));
  759.         CloseScreen(ascr);
  760.         CloseLibrary(DiskfontBase);
  761.         CloseLibrary((Library *)GfxBase);
  762.         CloseLibrary((Library *)IntuitionBase);
  763.         CloseLibrary((Library *)DOSBase);
  764.     }
  765. }
  766.  
  767. void close_window(LWindow win)
  768. {
  769.     IWindow * w=(IWindow *)win;
  770.     DPRINT(("- close_window(%lx)\n",w));
  771.     if(w==NULL) return;
  772.     wsigmask&=~(1<<w->win->UserPort->mp_SigBit);
  773.     warray[--warrcnt]=NULL;    // #####
  774.     FreeRaster(w->trb,w->win->MaxWidth,w->win->MaxHeight);
  775.     CloseWindow(w->win);
  776. }
  777.  
  778. void clear_window(LWindow win)
  779. {
  780.     DPRINT(("- clear_window(%lx,%d)\n",win));
  781.     IWindow * w=(IWindow *)win;
  782.     int save_mode=set_mode(0);
  783.     SetRast(w->rp,w->bg_col);
  784.     int save_col=set_color(1);
  785.     rectangle(win,0,0,window_width(win)-1,window_height(win)-1);
  786.     set_color(save_col);
  787.     set_mode(save_mode);
  788. }
  789.  
  790.  
  791. void pixel(LWindow win, int x, int y)
  792. {
  793.     DPRINT(("void pixel(LWindow win, int x, int y)\n"));
  794.     IWindow * w=(IWindow *)win;
  795.     UPDATE(win);
  796.     WritePixel(w->rp,x,y);
  797. }
  798.  
  799.  
  800. void pixels(LWindow win, int n, int *x, int *y)
  801. {
  802.     DPRINT(("void pixels(LWindow win, int n, int *x, int *y)\n"));
  803.     int i;
  804.     for(i=0; i<n; i++)
  805.         pixel(win,x[i],y[i]);
  806. }
  807.  
  808.  
  809. void line(LWindow win, int x1, int y1, int x2, int y2)
  810. {
  811.     DPRINT(("- line()\n"));
  812.     IWindow * w=(IWindow *)win;
  813.     UPDATE(win);
  814.     ALine(w->rp,x1,y1,x2,y2);
  815. }
  816.  
  817.  
  818. void rectangle(LWindow win, int x1, int y1, int x2, int y2)
  819. {
  820.     IWindow * w=(IWindow *)win;
  821.     DPRINT(("- rectangle()\n"));
  822.     int t;
  823.     if(x1>x2) {t=x1;x1=x2;x2=t;}
  824.     if(y1>y2) {t=y1;y1=y2;y2=t;}
  825.     UPDATE(win);
  826.     ARect(w->rp,x1,y1,x2,y2);
  827. }
  828.  
  829.  
  830. void box(LWindow win, int x1, int y1, int x2, int y2)
  831. {
  832.     DPRINT(("void box(LWindow win, int x1, int y1, int x2, int y2)\n"));
  833.     IWindow * w=(IWindow *)win;
  834.     if(x1>x2) {int x=x1;x1=x2;x2=x;}
  835.     if(y1>y2) {int y=y1;y1=y2;y2=y;}
  836.     UPDATE(win);
  837.     RectFill(w->rp,x1,y1,x2,y2);
  838. }
  839.  
  840.  
  841. void arc(LWindow win, int x0, int y0, int r1, int r2, double start, double angle)
  842. {
  843.     DPRINT(("- arc(start=%f,angle=%f)\n",start,angle));
  844.     IWindow * w=(IWindow *)win;
  845.     UPDATE(win);
  846.     if (angle < 0)
  847.     {
  848.         start += angle;
  849.         angle *= -1;
  850.     }
  851.     while(start<0)
  852.         start+=2*M_PI;
  853.     double x,y,rx,ry,a,s,end=start+angle;
  854.     //start=0;end=M_PI/2.0;
  855.     s=M_PI/180;
  856.     x=x0;y=y0;rx=r1;ry=r2;
  857.     Move(w->rp,int(x0+r1*cos(start)),int(y0-r2*sin(start)));
  858.     for(a=start+s;a<end;a+=s)
  859.     {
  860.         Draw(w->rp,int(x0+r1*cos(a)),int(y0-r2*sin(a)));
  861.     }
  862.     Draw(w->rp,int(x0+r1*cos(end)),int(y0-r2*sin(end)));
  863. }
  864.  
  865.  
  866. void circle(LWindow win, int x0, int y0, int r)
  867. {
  868.     DPRINT(("void circle(LWindow win, int x0, int y0, int r)\n"));
  869.     IWindow * w=(IWindow *)win;
  870.     UPDATE(win);
  871.     const int steps=int(sqrt(r*20.0));
  872.     if(pr.Picasso_Kludge && a_drawmode==COMPLEMENT)
  873.     {
  874.         Move(w->rp,x0+r,y0);
  875.         WritePixel(w->rp,x0+r,y0);
  876.         double as=6.283185/steps;
  877.         for(int i=1;i<=steps;i++)
  878.         {
  879.             double a=as*i;
  880.             Draw(w->rp,x0+int(r*cos(a)),y0+int(r*sin(a)));
  881.         }
  882.     }
  883.     else
  884.     {
  885.         DrawEllipse(w->rp,x0,y0,r,r);
  886.     }
  887. }
  888.  
  889. void ellipse(LWindow win, int x0, int y0, int r1, int r2)
  890. {
  891.     DPRINT(("void ellipse(LWindow win, int x0, int y0, int r1, int r2)\n"));
  892.     IWindow * w=(IWindow *)win;
  893.     UPDATE(win);
  894.     DrawEllipse(w->rp,x0,y0,r1,r2);
  895. }
  896.  
  897.  
  898. void fill_arc(LWindow win, int x0, int y0, int r1, int r2, double start, double angle)
  899. {
  900.     DPRINT(("void fill_arc(LWindow win, int x0, int y0, int r1, int r2, double start, double angle)\n"));
  901.     IWindow * w=(IWindow *)win;
  902.     UPDATE(win);
  903.     DrawEllipse(w->rp,x0,y0,r1,r2);
  904.     DrawEllipse(w->rp,x0,y0,r1-2,r2-2);
  905. }
  906.  
  907. void fill_circle(LWindow win, int x0, int y0, int r)
  908. {
  909.     DPRINT(("void fill_circle(LWindow win, int x0, int y0, int r)\n"));
  910.     IWindow * w=(IWindow *)win;
  911.     UPDATE(win);
  912.     if(pr.Picasso_Kludge && a_drawmode==COMPLEMENT)
  913.     {
  914.         for(int i=1;i<=r;i++)
  915.             DrawEllipse(w->rp,x0,y0,i,i);
  916.     }
  917.     else
  918.     {
  919.         UBYTE buf[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  920.         struct AreaInfo ai;
  921.         InitArea(&ai,buf,4);
  922.         w->rp->AreaInfo=&ai;
  923.         AreaEllipse(w->rp,x0,y0,r,r);
  924.         AreaEnd(w->rp);
  925.     }
  926. }
  927.  
  928.  
  929. void fill_ellipse(LWindow win, int x0, int y0, int r1, int r2)
  930. {
  931.     DPRINT(("void fill_ellipse(LWindow win, int x0, int y0, int r1, int r2)\n"));
  932.     IWindow * w=(IWindow *)win;
  933.     UPDATE(win);
  934.     if(pr.Picasso_Kludge && a_drawmode==COMPLEMENT)
  935.     {
  936.         DrawEllipse(w->rp,x0,y0,r1,r2);
  937.         int maxr=r1;if(r2>r1)maxr=r2;
  938.         for(int i=1;i<maxr;i++)
  939.             DrawEllipse(w->rp,x0,y0,i<r1?i:r1,i<r2?i:r2);
  940.     }
  941.     else
  942.     {
  943.         UBYTE buf[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  944.         struct AreaInfo ai;
  945.         InitArea(&ai,buf,4);
  946.         w->rp->AreaInfo=&ai;
  947.         AreaEllipse(w->rp,x0,y0,r1,r2);
  948.         AreaEnd(w->rp);
  949.     }
  950. }
  951.  
  952.  
  953. void polygon(LWindow win,int n,int * xcoord,int * ycoord)
  954. {
  955.     DPRINT(("void polygon(LWindow win,int n,int * xcoord,int * ycoord)\n"));
  956.     int i;
  957.     EPRINT(("- x_draw_polygon(%d)\n",n));
  958.     for(i=0;i<n-1;i++) 
  959.     {
  960.         line(win,xcoord[i],ycoord[i], xcoord[i+1],ycoord[i+1]);
  961.     }
  962.     line(win,xcoord[n-1],ycoord[n-1],xcoord[0],ycoord[0]);
  963. }
  964.  
  965. void fill_polygon(LWindow win, int n, int *xcoord, int *ycoord)
  966. {
  967.     DPRINT(("void fill_polygon(LWindow win, int n, int *xcoord, int *ycoord)\n"));
  968.     IWindow * w=(IWindow *)win;
  969.     UPDATE(win);
  970.     struct AreaInfo ai;
  971.     UBYTE *buf;
  972.     int i;
  973.     EPRINT(("- x_draw_filled_polygon(#%d,col:%d)\n",n,col));
  974.     if(pr.Picasso_Kludge && a_drawmode==COMPLEMENT)
  975.     {
  976.         polygon(win,n,xcoord,ycoord);
  977.     }
  978.     else
  979.     {
  980.         if(buf=(UBYTE *)AllocVec((n+1)*5,MEMF_CLEAR))
  981.         {
  982.             InitArea(&ai,buf,(n+1));
  983.             w->rp->AreaInfo=&ai;
  984.             AreaMove(w->rp,xcoord[0],ycoord[0]);
  985.             for(i=1;i<n;i++) 
  986.             {
  987.                 AreaDraw(w->rp,xcoord[i],ycoord[i]);
  988.             }
  989.             AreaEnd(w->rp);
  990.             FreeVec(buf);
  991.         }
  992.     }
  993. }
  994.  
  995.  
  996. void put_text(LWindow win, int x, int y, const char* s, int l, int opaque)
  997. {
  998.     DPRINT(("- put_text(xy:%d,%d,\"%lx:%s\",opaque:%d)\n",x,y,s,s,opaque));
  999.     IWindow * w=(IWindow *)win;
  1000.     UPDATE(win);
  1001.     if (l>strlen(s)) l=strlen(s);
  1002.     if(opaque)
  1003.     {
  1004.         SetDM(w->rp,JAM2);
  1005.     }
  1006.     else
  1007.     {
  1008.         SetDM(w->rp,a_drawmode);
  1009.     }
  1010.     Move(w->rp,x,y+w->rp->Font->tf_Baseline);
  1011.     Text(w->rp,s,l);
  1012.     ResetDMFg(w->rp);
  1013. }
  1014.  
  1015. void put_text(LWindow win, int x, int y, const char* s, int opaque)
  1016. {
  1017.     DPRINT(("- put_text(xy:%d,%d,\"%lx:%s\",opaque:%d)\n",x,y,s,s,opaque));
  1018.     IWindow * w=(IWindow *)win;
  1019.     UPDATE(win);
  1020.     if(opaque)
  1021.     {
  1022.         SetDM(w->rp,JAM2);
  1023.     }
  1024.     else
  1025.     {
  1026.         SetDM(w->rp,a_drawmode);
  1027.     }
  1028.     Move(w->rp,x,y+w->rp->Font->tf_Baseline);
  1029.     Text(w->rp,s,strlen(s));
  1030.     ResetDMFg(w->rp);
  1031. }
  1032.  
  1033.  
  1034. void put_ctext(LWindow win, int x, int y, const char* s, int opaque)
  1035. {
  1036.     DPRINT(("void put_ctext(LWindow win, int x, int y, const char* s, int opaque)\n"));
  1037.     x -= text_width(s)/2;
  1038.     y -= text_height(s)/2;
  1039.     put_text(win,x,y,s,opaque);
  1040. }
  1041.  
  1042.  
  1043. void show_coordinates(LWindow win, const char* s)
  1044. {
  1045.     DPRINT(("- show_coordinates(%s)\n",s));
  1046.     put_text(win,window_width(win)-160,2,s,1);
  1047. }
  1048.  
  1049.  
  1050. int text_width(const char* s)
  1051. {
  1052.     struct TextExtent te;
  1053.     TextExtent(&allrp,s,strlen(s),&te);
  1054.     DPRINT(("- text_width(%s)=%d\n",s,te.te_Width));
  1055.     return te.te_Width;
  1056. }
  1057.  
  1058.  
  1059. int text_height(const char* s)
  1060. {
  1061.     struct TextExtent te;
  1062.     TextExtent(&allrp,s,strlen(s),&te);
  1063.     DPRINT(("- text_height(%s)=%d\n",s,te.te_Height));
  1064.     return te.te_Height;
  1065. }
  1066.  
  1067.  
  1068. void copy_pixrect(LWindow win, int x1, int y1, int x2, int y2, int x, int y)
  1069. {
  1070.     CPRINT(("void copy_rect(LWindow win, int x1, int y1, int x2, int y2, int x, int y)\n"));
  1071. #if 0
  1072.   int save=set_mode(0);   /* src-mode */
  1073.   Pixmap P=XCreatePixmap(display,win,x2-x1,y2-y1,DefaultDepth(display,screen));
  1074.   XCopyArea(display,win,P,gc, x1,y1,x2-x1,y2-y1,0,0);
  1075.   XCopyArea(display,P,win,gc,0,0,x2-x1,y2-y1,x,y);
  1076.   set_mode(save);
  1077. #endif
  1078. }
  1079.  
  1080. void insert_bitmap(LWindow win, int width, int height, char* data)
  1081. {
  1082.     CPRINT(("void insert_bitmap(LWindow win, int width, int height, char* data)\n"));
  1083. #if 0
  1084.  int save=set_mode(0);
  1085.   Pixmap P=XCreatePixmapFromBitmapData(display,win,data,width,height,
  1086.                                        BlackPixel(display,screen),
  1087.                                        WhitePixel(display,screen),
  1088.                                        DefaultDepth(display,screen));
  1089.   XCopyArea(display,P,win,gc,0,0,width,height,0,0);
  1090.   set_mode(save);
  1091.  #endif
  1092. }
  1093.  
  1094.  
  1095. void set_header(LWindow win, const char* s)
  1096. {
  1097.     DPRINT(("- set_header(%lx,%s)\n",win,s));
  1098.     IWindow * w=(IWindow *)win;
  1099.     strncpy(w->Title,s,TITLELEN);
  1100.     //SetWindowTitles(w->win,(UBYTE *)w->Title,(UBYTE *)~0);
  1101.     RefreshWindowFrame(w->win);
  1102. }
  1103.  
  1104.  
  1105. int set_color(int col)
  1106. {
  1107.     //int save=COLOR;
  1108.     DPRINT(("- set_color(%ld)=%ld [pen=%ld]\n",col,save,color_pix[col]));
  1109.     COLOR=col;
  1110.     SetFg(&allrp,color_pix[col]);
  1111.     UPDATEON;
  1112.     return col;
  1113. }
  1114.  
  1115.  
  1116. int set_mode(int m)
  1117. {
  1118.     int save=MODE;
  1119.     MODE=m;
  1120.     switch(m)
  1121.     {
  1122.         case 0:
  1123.             a_drawmode=JAM1;
  1124.             break;
  1125.         case 1:
  1126.             a_drawmode=COMPLEMENT;
  1127.             break;
  1128.         case 2:
  1129.             a_drawmode=COMPLEMENT; /* was: OR */
  1130.             break;
  1131.         default:
  1132.             break;
  1133.     }
  1134.     DPRINT(("- set_mode(%d)=%d [a_dm=%d]\n",m,save,a_drawmode));
  1135.     SetDM(&allrp,a_drawmode);
  1136.     UPDATEON;
  1137.     return save;
  1138. }
  1139.  
  1140.  
  1141. int load_text_font(const char* font_name)
  1142. {
  1143.     CPRINT(("int load_text_font(%s)\n",font_name));
  1144.     return true;
  1145. #if 0
  1146.  XFontStruct* fp=XLoadQueryFont(display,font_name);
  1147.   if (fp)
  1148.   { text_font=fp;
  1149.     text_char_width=get_char_width(fp);
  1150.    }
  1151.   return (fp != NULL);
  1152.  #endif
  1153. }
  1154.  
  1155.  
  1156. int load_bold_font(const char* font_name)
  1157. {
  1158.     CPRINT(("int load_bold_font(%s)\n",font_name));
  1159.     return true;
  1160. #if 0
  1161.  XFontStruct* fp=XLoadQueryFont(display,font_name);
  1162.   if (fp)
  1163.   { bold_font=fp;
  1164.     bold_char_width=get_char_width(fp);
  1165.    }
  1166.   return (fp != NULL);
  1167.  #endif
  1168. }
  1169.  
  1170.  
  1171. int load_message_font(const char* font_name)
  1172. {
  1173.     CPRINT(("int load_message_font(%s)\n",font_name));
  1174.     return true;
  1175. #if 0
  1176.  XFontStruct* fp=XLoadQueryFont(display,font_name);
  1177.   if (fp)
  1178.   { mesg_font=fp;
  1179.     mesg_char_width=get_char_width(fp);
  1180.    }
  1181.   return (fp != NULL);
  1182.  #endif
  1183. }
  1184.  
  1185.  
  1186. int set_font(const char *fname)
  1187. {
  1188.     CPRINT(("int set_font(%s)\n",fname));
  1189.     return true;
  1190. #if 0
  1191.  XFontStruct* fp=XLoadQueryFont(display,fname);
  1192.   if (fp)
  1193.   { current_font=fp;
  1194.     current_char_width=get_char_width(fp);
  1195.     gc_val.font=fp->fid;
  1196.     XChangeGC(display,gc,GCFont,&gc_val);
  1197.    }
  1198.   return (fp != NULL);
  1199.  #endif
  1200. }
  1201.  
  1202.  
  1203. static void SetFonts(struct TextFont * f)
  1204. {
  1205.     IWindow *w;
  1206.     forall_windows(w)
  1207.         SetFont(w->rp,f);
  1208. }
  1209.  
  1210. void set_text_font(void)
  1211. {
  1212.     DPRINT(("void set_text_font(void)\n"));
  1213.     if(current_font!=text_font)
  1214.     {
  1215.         current_font=text_font;
  1216.         SetFonts(current_font);
  1217.         current_char_width=text_char_width;
  1218.     }
  1219. }
  1220.  
  1221.  
  1222. void set_bold_font(void)
  1223. {
  1224.     DPRINT(("void set_bold_font(void)\n"));
  1225.     if(current_font!=bold_font)
  1226.     {
  1227.         current_font=bold_font;
  1228.         SetFonts(current_font);
  1229.         current_char_width=bold_char_width;
  1230.     }
  1231. }
  1232.  
  1233.  
  1234. void set_message_font(void)
  1235. {
  1236.     DPRINT(("void set_message_font(void)\n"));
  1237.     if(current_font!=mesg_font)
  1238.     {
  1239.         current_font=mesg_font;
  1240.         SetFonts(current_font);
  1241.         current_char_width=mesg_char_width;
  1242.     }
  1243. }
  1244.  
  1245.  
  1246.  
  1247. int set_line_width(int w)
  1248. {
  1249.     int save=a_linewidth;
  1250.     a_linewidth=w;
  1251.     DPRINT(("- set_line_width(%d)=%d\n",w,save));
  1252.     UPDATEON;
  1253.     return save;
  1254. }
  1255.  
  1256.  
  1257. int set_line_style(int s)
  1258. {
  1259.     int save=LINESTYLE;
  1260.     LINESTYLE=s;
  1261.     switch(s)
  1262.     {
  1263.         case 0:
  1264.             a_linestyle=SOLID;
  1265.             break;
  1266.         case 1:
  1267.             a_linestyle=DASHED;
  1268.             break;
  1269.         case 2:
  1270.             a_linestyle=DOTTED;
  1271.             break;
  1272.         default:
  1273.             a_linestyle=SOLID;
  1274.             break;
  1275.     }
  1276.     SetLineStyle(&allrp,a_linestyle);
  1277.     DPRINT(("- set_line_style(%d)=%d\n",s,save));
  1278.     UPDATEON;
  1279.     return save;
  1280. }
  1281.  
  1282.  
  1283. void set_read_gc(void)
  1284. {
  1285.     DPRINT(("void set_read_gc(void)\n"));
  1286. #if 0
  1287. XGCValues gc_val0;
  1288.  gc_val0.function=GXxor;
  1289.  gc_val0.foreground=BlackPixel(display,screen);
  1290.  gc_val0.line_style=LineSolid;
  1291.  gc_val0.line_width=1;
  1292.  XChangeGC(display,gc,GCForeground|GCFunction|GCLineStyle|GCLineWidth,&gc_val0);
  1293.  flush_display();
  1294. #endif
  1295. }
  1296.  
  1297. void reset_gc(void)
  1298. {
  1299.     DPRINT(("void reset_gc(void)\n"));
  1300. #if 0
  1301.  XChangeGC(display,gc,GCForeground|GCFunction|GCLineStyle|GCLineWidth,&gc_val);
  1302.   flush_display();
  1303. #endif
  1304. }
  1305.  
  1306.  
  1307. int window_height(LWindow win)
  1308. {
  1309.     IWindow *w=(IWindow *)win;
  1310.     DPRINT(("- window_height(%lx)=%d\n",win,w->win->GZZHeight));
  1311.     return w->win->GZZHeight;
  1312. }
  1313.  
  1314. int window_width(LWindow win)
  1315. {
  1316.     IWindow *w=(IWindow *)win;
  1317.     DPRINT(("- window_width(%lx)=%d\n",win,w->win->GZZWidth));
  1318.     return w->win->GZZWidth;
  1319. }
  1320.  
  1321.  
  1322. void window_position(LWindow win, int *x, int *y)
  1323. {
  1324.     DPRINT(("void window_position(LWindow win, int *x, int *y)\n"));
  1325.     IWindow *w=(IWindow *)win;
  1326.     *x=w->win->LeftEdge;
  1327.     *y=w->win->TopEdge;
  1328. }
  1329.  
  1330.  
  1331. static int  handle_event(LWindow *win,int *val,int *x,int *y,unsigned long *t)
  1332. {
  1333.     DPRINT(("- handle_event()\n"));
  1334.  
  1335.     int kind=no_event;
  1336.     *win=(LWindow)ieventwin;
  1337.     *val=0;
  1338.     *x=ievent.MouseX-ieventwin->borl;
  1339.     *y=ievent.MouseY-ieventwin->bort;
  1340.     *t=ievent.Seconds;
  1341.     UWORD qual=ievent.Qualifier;
  1342.  
  1343.     switch(ievent.Class)
  1344.     {
  1345.         //case ConfigureNotify:
  1346.         //    kind=configure_event;
  1347.         //    *x=event.xconfigure.width;
  1348.         //    *y=event.xconfigure.height;
  1349.         //    break;
  1350.         case IDCMP_NEWSIZE:
  1351.             *x=ieventwin->win->GZZWidth;
  1352.             *y=ieventwin->win->GZZHeight;
  1353.             kind=configure_event;
  1354.             break;
  1355.  
  1356.         //case DestroyNotify:
  1357.         //    kind=destroy_event;
  1358.         //    break;
  1359.  
  1360.         case IDCMP_MOUSEBUTTONS:
  1361.             switch(ievent.Code)
  1362.             {
  1363.                 case SELECTUP:
  1364.                     *val=1;
  1365.                     if (qual&IEQUALIFIER_CONTROL)
  1366.                         *val +=3;
  1367.                     if (qual&(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT))
  1368.                         *val=-*val;
  1369.                     kind=button_release_event;
  1370.                     break;
  1371.                 case MIDDLEUP:
  1372.                     *val=2;
  1373.                     if (qual&IEQUALIFIER_CONTROL)
  1374.                         *val +=3;
  1375.                     if (qual&(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT))
  1376.                         *val=-*val;
  1377.                     kind=button_release_event;
  1378.                     break;
  1379.                 case MENUUP:
  1380.                     *val=3;
  1381.                     if (qual&IEQUALIFIER_CONTROL)
  1382.                         *val +=3;
  1383.                     if (qual&(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT))
  1384.                         *val=-*val;
  1385.                     kind=button_release_event;
  1386.                     break;
  1387.                 case SELECTDOWN:
  1388.                     *val=1;
  1389.                     if (qual&IEQUALIFIER_CONTROL)
  1390.                         *val +=3;
  1391.                     if (qual&(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT))
  1392.                         *val=-*val;
  1393.                     kind=button_press_event;
  1394.                     break;
  1395.                 case MIDDLEDOWN:
  1396.                     *val=2;
  1397.                     if (qual&IEQUALIFIER_CONTROL)
  1398.                         *val +=3;
  1399.                     if (qual&(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT))
  1400.                         *val=-*val;
  1401.                     kind=button_press_event;
  1402.                     break;
  1403.                 case MENUDOWN:
  1404.                     *val=3;
  1405.                     if (qual&IEQUALIFIER_CONTROL)
  1406.                         *val +=3;
  1407.                     if (qual&(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT))
  1408.                         *val=-*val;
  1409.                     kind=button_press_event;
  1410.                     break;
  1411.             }
  1412.             break;
  1413.         case IDCMP_RAWKEY:            // F1,F2,F3 emulate LMB,MMB,RMB
  1414.             switch(ievent.Code)
  1415.             {
  1416.                 case 80 | IECODE_UP_PREFIX:
  1417.                     *val=1;
  1418.                     if (qual&IEQUALIFIER_CONTROL)
  1419.                         *val +=3;
  1420.                     if (qual&(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT))
  1421.                         *val=-*val;
  1422.                     kind=button_release_event;
  1423.                     break;
  1424.                 case 81 | IECODE_UP_PREFIX:
  1425.                     *val=2;
  1426.                     if (qual&IEQUALIFIER_CONTROL)
  1427.                         *val +=3;
  1428.                     if (qual&(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT))
  1429.                         *val=-*val;
  1430.                     kind=button_release_event;
  1431.                     break;
  1432.                 case 82 | IECODE_UP_PREFIX:
  1433.                     *val=3;
  1434.                     if (qual&IEQUALIFIER_CONTROL)
  1435.                         *val +=3;
  1436.                     if (qual&(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT))
  1437.                         *val=-*val;
  1438.                     kind=button_release_event;
  1439.                     break;
  1440.                 case 80:
  1441.                     *val=1;
  1442.                     if (qual&IEQUALIFIER_CONTROL)
  1443.                         *val +=3;
  1444.                     if (qual&(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT))
  1445.                         *val=-*val;
  1446.                     kind=button_press_event;
  1447.                     break;
  1448.                 case 81:
  1449.                     *val=2;
  1450.                     if (qual&IEQUALIFIER_CONTROL)
  1451.                         *val +=3;
  1452.                     if (qual&(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT))
  1453.                         *val=-*val;
  1454.                     kind=button_press_event;
  1455.                     break;
  1456.                 case 82:
  1457.                     *val=3;
  1458.                     if (qual&IEQUALIFIER_CONTROL)
  1459.                         *val +=3;
  1460.                     if (qual&(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT))
  1461.                         *val=-*val;
  1462.                     kind=button_press_event;
  1463.                     break;
  1464.             }
  1465.             break;
  1466.  
  1467.         //case MotionNotify:
  1468.         //    *x=event.xmotion.x;
  1469.         //    *y=event.xmotion.y;
  1470.         //    kind=motion_event;
  1471.         //    break;
  1472.         case IDCMP_MOUSEMOVE:
  1473.             kind=motion_event;
  1474.             break;
  1475.  
  1476.         //case KeyPress:
  1477.         //    *x=event.xmotion.x;
  1478.         //    c=0;
  1479.         //    XLookupString((XKeyEvent*)&event,&c,1, &keysym, &status);
  1480.         //    *val=c;
  1481.         //    kind=key_press_event;
  1482.         //    break;
  1483.         case IDCMP_VANILLAKEY:
  1484.             *val=ievent.Code;
  1485.             kind=key_press_event;
  1486.             break;
  1487.     }
  1488.     return kind;
  1489. }
  1490.  
  1491.  
  1492. int check_next_event(LWindow *win, int *val, int *x, int *y, unsigned long *t)
  1493. {
  1494.     // non-blocking
  1495.     IWindow *w;
  1496.     DPRINT(("- check_next_event(w=%lx)\n",w));
  1497.  
  1498.     struct IntuiMessage* msg;
  1499.  
  1500.     if(eventagain)
  1501.     {
  1502.         eventagain=FALSE;
  1503.         return handle_event(win,val,x,y,t);
  1504.     }
  1505.  
  1506.     forall_windows(w)
  1507.     {
  1508.         msg=(struct IntuiMessage *)GetMsg(w->win->UserPort);
  1509.         if(msg)
  1510.         {
  1511.             memcpy(&ievent,msg,sizeof(struct IntuiMessage));
  1512.             ieventwin=w;
  1513.             ReplyMsg((struct Message *)msg);
  1514.             return handle_event(win,val,x,y,t);
  1515.         }
  1516.     }
  1517.     return no_event;
  1518. }
  1519.  
  1520. int get_next_event(LWindow *win, int *val, int *x, int *y, unsigned long *t)
  1521. {
  1522.     // blocking
  1523.     IWindow *w;
  1524.     DPRINT(("- get_next_event()\n"));
  1525.  
  1526.     ULONG signals;
  1527.     struct IntuiMessage* msg;
  1528.  
  1529.     if(eventagain)
  1530.     {
  1531.         eventagain=FALSE;
  1532.         return handle_event(win,val,x,y,t);
  1533.     }
  1534.  
  1535.     for(;;)
  1536.     {
  1537.         forall_windows(w)
  1538.         {
  1539.             msg=(struct IntuiMessage *)GetMsg(w->win->UserPort);
  1540.             if(msg)
  1541.             {
  1542.                 memcpy(&ievent,msg,sizeof(struct IntuiMessage));
  1543.                 ieventwin=w;
  1544.                 ReplyMsg((struct Message *)msg);
  1545.                 return handle_event(win,val,x,y,t);
  1546.             }
  1547.         }
  1548.         signals=Wait(wsigmask);
  1549.     }
  1550. }
  1551.  
  1552. void put_back_event(void)
  1553. {
  1554.     DPRINT(("void put_back_event(void)\n"));
  1555.     eventagain=TRUE;
  1556.     //XPutBackEvent(display,&event);
  1557. }
  1558.